var User = require('./../../models/User.js');
var City = require('./../../models/City.js');
var Project = require('./../../models/Project.js');

var request = require('request').defaults({
    json: true
});
var config = require('./../../config.json');
var q = require('q');
var redis = require('redis');

/*
@prjList:Array<number>
根据提供的prjList获取City List 数据
*/
exports._getProjectsByList = function (prjList) {
    var defer = q.defer();
    var redisClient = redis.createClient();
    var redisKey = config.redisNamespace + ":allBackgroundProjects";
    redisClient.get(redisKey, function (err, reply) {
        redisClient.quit();
        if (err || !reply) {
            defer.resolve([]);
        } else {
            var prjs = JSON.parse(JSON.parse(reply));
            var results = prjs.filter(function (prj) {
                return prjList.indexOf(prj.prjId) >= 0;
            });
            var data = City.parseProjectsToCityList(results);
            defer.resolve(data.cityList[0]);
        }
    });
    return defer.promise;
};

/*
对单个项目进行时间范围内的统计查询
*/
exports.stasticChange = function (prjId, startDate, endDate) {
    var _this = this;
    var prj = new Project(prjId);
    var sum = 0;
    var defer = q.defer();
    prj.getMonitorPoints().then(function (monitorPoints) {
        prj.monitorPoints = monitorPoints;
        monitorPoints.forEach(function (mGroup) {
            sum += mGroup.locationPoints.length;
        });
        console.log('一共有%s组点', sum);
        var index = 0;
        prj.monitorPoints.forEach(function (mGroup) {
            mGroup.locationPoints = mGroup.locationPoints.filter(function (locationPoint) {
                return locationPoint.points.length > 0;
            });
            mGroup.locationPoints.forEach(function (locationPoint) {
                locationPoint.count = locationPoint.points.length;
                delete locationPoint.points;
                _this._recentChange(prj, mGroup.monitorType, locationPoint.location, startDate, endDate)
                    .then((function (locationPoint) {
                        return function (change) {
                            delete change.lastValues;
                            delete change.curValues;
                            locationPoint.change = change;
                            locationPoint.hasValue = change.maxPointThisTime ? true : false;
                            index++;
                            if (index === sum) {
                                defer.resolve(prj.monitorPoints);
                            }
                        };
                    })(locationPoint))
            })
        });

    });
    return defer.promise;

};

exports._recentChange = function (prj, monitorType, location, startDate, endDate) {
    if (monitorType === '收敛') {//如果是收敛就多查一下设计值
        var defer = q.defer();
        return q.all([prj.getRecentChanges({
            prjId: prj.prjId,
            type: monitorType,
            location: location,
            time1: startDate,
            time2: endDate,
            fuzzy: 1,
            isDesign: false
        }), prj.getRecentChanges({
            prjId: prj.prjId,
            type: monitorType,
            location: location,
            time1: startDate,
            time2: endDate,
            fuzzy: 1,
            isDesign: true
        })]).then(function (chordDatas) {
            var data = chordDatas[0];
            data.maxDesignPointAllTheTime = chordDatas[1].maxPointAllTheTime;
            // defer.resolve(data);
            return data;
        });
        // return defer.promise;
    } else {
        return prj.getRecentChanges({
            prjId: prj.prjId,
            type: monitorType,
            location: location,
            time1: startDate,
            time2: endDate,
            fuzzy: 1,
            isDesign: false
        });
    }
};




